﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace DockSample
{
    class InquiryUtil
    {
        //
        public static DummyModelForm getOriginalModelFormByDiagram(OriginalDiagram diagram)
        {
            DummyModelForm result = null;

            foreach (DummyModelForm form in SystemVar.mainForm.modelFormList)
            {
                foreach (OriginalDiagram diag in form.diagramList)
                {
                    if (diagram.id.Equals(diag.id))
                    {
                        foreach (OriginalDiagram origDiag in form.diagramList)
                        {
                            origDiag.IsSelected = false;
                        }
                        diag.IsSelected = true;
                        result = form;
                        break;
                    }
                }
                if (result != null)
                {
                    break;
                }
            }
            return result;
        }
        //
        public static DummyModelForm getOriginalModelFormByAttr(Attribute attr)
        {
            DummyModelForm result = null;

            foreach (DummyModelForm form in SystemVar.mainForm.modelFormList)
            {
                foreach (OriginalDiagram diag in form.diagramList)
                {
                    ObjectDiagram objectDiag = (ObjectDiagram)diag;
                    foreach (Attribute newAttr in objectDiag.AttributeList)
                    {
                        if (attr.id.Equals(newAttr.id))
                        {
                            foreach (OriginalDiagram origDiag in form.diagramList)
                            {
                                origDiag.IsSelected = false;
                            }
                            objectDiag.IsSelected = true;
                            result = form;
                            break;
                        }
                    }
                    if (result != null)
                    {
                        break;
                    }
                }
                if (result != null)
                {
                    break;
                }
            }

            return result;
        }
        //
        public static DummyModelForm getOriginalModelFormByMethod(Method method)
        {
            DummyModelForm result = null;

            foreach (DummyModelForm form in SystemVar.mainForm.modelFormList)
            {
                foreach (OriginalDiagram diag in form.diagramList)
                {
                    ObjectDiagram objectDiag = (ObjectDiagram)diag;
                    foreach (Method newMethod in objectDiag.MethodList)
                    {
                        if (method.id.Equals(newMethod.id))
                        {
                            foreach (OriginalDiagram origDiag in form.diagramList)
                            {
                                origDiag.IsSelected = false;
                            }
                            objectDiag.IsSelected = true;
                            result = form;
                            break;
                        }
                    }
                    if (result != null)
                    {
                        break;
                    }
                }
                if (result != null)
                {
                    break;
                }
            }

            return result;
        }
        //通过面板得到其所属的高阶对象
        public static HighOrderObjectDiagram getHighOrderObjectByNextLevelModelForm(DummyModelForm form)
        {
            HighOrderObjectDiagram result = null;
            foreach (DummyModelForm tmpForm in SystemVar.mainForm.modelFormList)
            {
                foreach (OriginalDiagram diag in tmpForm.diagramList)
                {
                    if(diag is HighOrderObjectDiagram &&((HighOrderObjectDiagram)diag).HasModelForm&&((HighOrderObjectDiagram)diag).NewModelForm.id.Equals(form.id))
                    {
                        result = (HighOrderObjectDiagram)diag;
                        break;
                    }
                }
                if (result != null)
                {
                    break;
                }
            }
            return result;
        }

        public static InformationSourceDiagram getInformationSourceByNextLevelModelForm(DummyModelForm form)
        {
            InformationSourceDiagram result = null;
            foreach (DummyModelForm tmpForm in SystemVar.mainForm.modelFormList)
            {
                foreach (OriginalDiagram diag in tmpForm.diagramList)
                {
                    if (diag is InformationSourceDiagram && ((InformationSourceDiagram)diag).HasModelForm && ((InformationSourceDiagram)diag).NewModelForm.id.Equals(form.id))
                    {
                        result = (InformationSourceDiagram)diag;
                        break;
                    }
                }
                if (result != null)
                {
                    break;
                }
            }
            return result;
        }

        public static HighStateDiagram getHighStateDiagramByNextLevelModelForm(DummyModelForm form)
        {
            HighStateDiagram result = null;
            foreach (DummyModelForm tmpForm in SystemVar.mainForm.modelFormList)
            {
                foreach (OriginalDiagram diag in tmpForm.diagramList)
                {
                    if (diag is HighStateDiagram && ((HighStateDiagram)diag).HasModelForm && ((HighStateDiagram)diag).NewModelForm.id.Equals(form.id))
                    {
                        result = (HighStateDiagram)diag;
                        break;
                    }
                }
                if (result != null)
                {
                    break;
                }
            }
            return result;
        }
        //
        public static DummyModelForm getOriginalModelFormByObjectDiagram(ObjectDiagram diagram)
        {
            DummyModelForm result = null;
            foreach (DummyModelForm tmpForm in SystemVar.mainForm.modelFormList)
            {
                foreach (OriginalDiagram tepDiag in tmpForm.diagramList)
                {
                    if (tepDiag.id.Equals(diagram.id))
                    {
                        result = (DummyModelForm)tmpForm;
                        break;
                    }
                }
                if (result != null)
                {
                    break;
                }
            }
            return result;
        }
        //
        public static  DummyModelForm getNextLevelModelFormByDiagram(OriginalDiagram diagram)
        {
            if (diagram is HighOrderObjectDiagram && ((HighOrderObjectDiagram)diagram).HasModelForm)
            {
                return ((HighOrderObjectDiagram)diagram).NewModelForm;
            }
            else if (diagram is InformationSourceDiagram && ((InformationSourceDiagram)diagram).HasModelForm)
            {
                return ((InformationSourceDiagram)diagram).NewModelForm;
            }
            else if (diagram is HighStateDiagram && ((HighStateDiagram)diagram).HasModelForm)
            {
                return ((HighStateDiagram)diagram).NewModelForm;
            }
            else
            {
                return null;
            }
        }
        //级联删除
        public static void deleteNextLevelModelForm(DummyModelForm modelForm)
        {
            foreach (OriginalDiagram diag in modelForm.diagramList)
            {
                //删除ObjectDiagram对应的InforsourceForm和StateTransForm
                if (diag is ObjectDiagram)
                {
                    deleteAssocInforSourceFormAndStateTransForm((ObjectDiagram)diag);
                }
                //垂直级联删除
                if ((diag is HighOrderObjectDiagram) && ((HighOrderObjectDiagram)diag).HasModelForm)
                {
                    ObjectModelForm tmpModelForm = ((HighOrderObjectDiagram)diag).NewModelForm;
                    deleteNextLevelModelForm(tmpModelForm);

                    deleteObjectModelForm(modelForm);
                }
                else if ((diag is InformationSourceDiagram) && ((InformationSourceDiagram)diag).HasModelForm)
                {
                    InformationFlowModelForm tmpModelForm = ((InformationSourceDiagram)diag).NewModelForm;
                    deleteNextLevelModelForm(tmpModelForm);

                    deleteInforModelForm(modelForm);
                }
                else if ((diag is HighStateDiagram) && ((HighStateDiagram)diag).HasModelForm)
                {
                    StateTransitionModelForm tmpModelForm = ((HighStateDiagram)diag).NewModelForm;
                    deleteNextLevelModelForm(tmpModelForm);

                    deleteStateModelForm(modelForm);
                }
            }
            deleteModelForm(modelForm);
        }

        private static void deleteModelForm(DummyModelForm modelForm)
        {
            if (modelForm is ObjectModelForm)
                deleteObjectModelForm(modelForm);
            else if (modelForm is InformationFlowModelForm)
                deleteInforModelForm(modelForm);
            else if ((modelForm is StateTransitionModelForm))
                deleteStateModelForm(modelForm);
        }

        private static void deleteObjectModelForm(DummyModelForm modelForm)
        {
            HighOrderObjectDiagram highOrderDiag = InquiryUtil.getHighOrderObjectByNextLevelModelForm(modelForm);
            if (highOrderDiag != null)
            {
                highOrderDiag.HasModelForm = false;
            }
            SystemVar.mainForm.modelFormList.Remove(modelForm);
            modelForm.Close();
        }

        private static void deleteInforModelForm(DummyModelForm modelForm)
        {
            InformationSourceDiagram highInforDiag = InquiryUtil.getInformationSourceByNextLevelModelForm(modelForm);
            if (highInforDiag != null)
            {
                highInforDiag.HasModelForm = false;
            }
            SystemVar.mainForm.modelFormList.Remove(modelForm);
            modelForm.Close();
        }

        private static void deleteStateModelForm(DummyModelForm modelForm)
        {
            HighStateDiagram highStateDiag = InquiryUtil.getHighStateDiagramByNextLevelModelForm(modelForm);
            if (highStateDiag != null)
            {
                highStateDiag.HasModelForm = false;
            }
            SystemVar.mainForm.modelFormList.Remove(modelForm);
            modelForm.Close();
        }

        public static void deleteAssocInforSourceFormAndStateTransForm(ObjectDiagram objDiag)
        {
            if (objDiag.AssocInforModelForm != null)
            {
                SystemVar.mainForm.modelFormList.Remove(objDiag.AssocInforModelForm);
                objDiag.AssocInforModelForm.Close();
            }
            if (objDiag.AssocStateModelForm != null)
            {
                SystemVar.mainForm.modelFormList.Remove(objDiag.AssocStateModelForm);
                objDiag.AssocStateModelForm.Close();
            }
        }
        //打印时的Form分层
        private static void setLeveNumByUpLevelForm(DummyModelForm form)
        {
            int levelNumbum = 0;
            foreach (DummyModelForm tmpForm in SystemVar.mainForm.modelFormList)
            {
                if (tmpForm is ObjectModelForm)
                {
                    ObjectModelForm tmpObjForm = (ObjectModelForm)tmpForm;
                    foreach (OriginalDiagram tmpDiag in tmpObjForm.diagramList)
                    {
                        if (tmpDiag is HighOrderObjectDiagram && ((HighOrderObjectDiagram)tmpDiag).HasModelForm
                            && ((HighOrderObjectDiagram)tmpDiag).NewModelForm.id.Equals(form.id))
                        {
                            levelNumbum = tmpForm.LevelNumbum;
                            break;
                        }
                    }
                }
                if (levelNumbum != 0)
                {
                    break;
                }
            }
            //顶层Form
            if (levelNumbum == 0)
            {
                form.LevelNumbum = 1;
            }
            else
            {
                form.LevelNumbum = levelNumbum + 1;
            }

            //return levelNumbum;
        }
        //
        public static void setLevelNumForAllObjectModelForm()
        {
            foreach (DummyModelForm form in SystemVar.mainForm.modelFormList)
            {
                setLeveNumByUpLevelForm(form);
            }
        }
        //滚动条
        public static Point getLeftBottomPointOfDummyModelForm(DummyModelForm form)
        {
            int xExtent = 0;
            int yExtent = 0;

            foreach (OriginalDiagram tmpDiag in form.diagramList)
            {
                xExtent = xExtent > tmpDiag.UpLeftpoint.X + tmpDiag.Width ? xExtent : tmpDiag.UpLeftpoint.X + tmpDiag.Width;
                yExtent = yExtent > tmpDiag.UpLeftpoint.Y + tmpDiag.Height ? yExtent : tmpDiag.UpLeftpoint.Y + tmpDiag.Height;
            }
            return new Point(xExtent, yExtent);
        }
    }
}
